package com.fy.market.persistent.respository.activity;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fy.market.activity.model.entity.UserRaffleOrderEntity;
import com.fy.market.activity.repository.IUserRaffleOrderRepository;
import com.fy.market.persistent.mapper.UserRaffleOrderMapper;
import com.fy.market.persistent.po.UserRaffleOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户抽奖订单表 仓储实现
 * @author fwq
 * @date 2025-10-11
 */
@Slf4j
@Repository
public class UserRaffleOrderRepository implements IUserRaffleOrderRepository {

    @Resource
    private UserRaffleOrderMapper userRaffleOrderMapper;

    @Override
    public UserRaffleOrderEntity queryUserRaffleOrderByOrderId(String orderId) {
        LambdaQueryWrapper<UserRaffleOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRaffleOrder::getOrderId, orderId);

        UserRaffleOrder userRaffleOrder = userRaffleOrderMapper.selectOne(queryWrapper);
        if (userRaffleOrder == null) {
            return null;
        }

        return convertToEntity(userRaffleOrder);
    }

    @Override
    public List<UserRaffleOrderEntity> queryUserRaffleOrderByUserIdAndActivityId(Integer userId, Integer activityId) {
        LambdaQueryWrapper<UserRaffleOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRaffleOrder::getUserId, userId)
                .eq(UserRaffleOrder::getActivityId, activityId)
                .orderByDesc(UserRaffleOrder::getCreateTime);

        List<UserRaffleOrder> userRaffleOrders = userRaffleOrderMapper.selectList(queryWrapper);
        return userRaffleOrders.stream()
                .map(this::convertToEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<UserRaffleOrderEntity> queryUserRaffleOrderByUserId(Integer userId) {
        LambdaQueryWrapper<UserRaffleOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRaffleOrder::getUserId, userId)
                .orderByDesc(UserRaffleOrder::getCreateTime);

        List<UserRaffleOrder> userRaffleOrders = userRaffleOrderMapper.selectList(queryWrapper);
        return userRaffleOrders.stream()
                .map(this::convertToEntity)
                .collect(Collectors.toList());
    }

    @Override
    public void createUserRaffleOrder(UserRaffleOrderEntity userRaffleOrderEntity) {
        UserRaffleOrder userRaffleOrder = new UserRaffleOrder();
        userRaffleOrder.setUserId(userRaffleOrderEntity.getUserId());
        userRaffleOrder.setActivityId(userRaffleOrderEntity.getActivityId());
        userRaffleOrder.setActivityName(userRaffleOrderEntity.getActivityName());
        userRaffleOrder.setStrategyId(userRaffleOrderEntity.getStrategyId());
        userRaffleOrder.setOrderId(userRaffleOrderEntity.getOrderId());
        userRaffleOrder.setOrderTime(userRaffleOrderEntity.getOrderTime());
        userRaffleOrder.setOrderStatus(userRaffleOrderEntity.getOrderStatus());
        userRaffleOrder.setCreateTime(new Date());
        userRaffleOrder.setUpdateTime(new Date());

        userRaffleOrderMapper.insert(userRaffleOrder);
        log.info("创建用户抽奖订单成功，订单ID：{}", userRaffleOrderEntity.getOrderId());
    }

    @Override
    public void updateOrderStatus(String orderId, Integer orderStatus) {
        LambdaUpdateWrapper<UserRaffleOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserRaffleOrder::getOrderId, orderId)
                .set(UserRaffleOrder::getOrderStatus, orderStatus)
                .set(UserRaffleOrder::getUpdateTime, new Date());

        int updateCount = userRaffleOrderMapper.update(null, updateWrapper);
        if (updateCount > 0) {
            log.info("更新订单状态成功，订单ID：{}，状态：{}", orderId, orderStatus);
        } else {
            log.warn("更新订单状态失败，订单ID：{}，状态：{}", orderId, orderStatus);
        }
    }

    @Override
    public void updateUserRaffleOrder(UserRaffleOrderEntity userRaffleOrderEntity) {
        UserRaffleOrder userRaffleOrder = new UserRaffleOrder();
        userRaffleOrder.setId(userRaffleOrderEntity.getId());
        userRaffleOrder.setUserId(userRaffleOrderEntity.getUserId());
        userRaffleOrder.setActivityId(userRaffleOrderEntity.getActivityId());
        userRaffleOrder.setActivityName(userRaffleOrderEntity.getActivityName());
        userRaffleOrder.setStrategyId(userRaffleOrderEntity.getStrategyId());
        userRaffleOrder.setOrderId(userRaffleOrderEntity.getOrderId());
        userRaffleOrder.setOrderTime(userRaffleOrderEntity.getOrderTime());
        userRaffleOrder.setOrderStatus(userRaffleOrderEntity.getOrderStatus());
        userRaffleOrder.setUpdateTime(new Date());

        userRaffleOrderMapper.updateById(userRaffleOrder);
        log.info("更新用户抽奖订单成功，订单ID：{}", userRaffleOrderEntity.getOrderId());
    }

    @Override
    public boolean isOrderExists(String orderId) {
        LambdaQueryWrapper<UserRaffleOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRaffleOrder::getOrderId, orderId);

        Long count = userRaffleOrderMapper.selectCount(queryWrapper);
        return count > 0;
    }

    /**
     * PO转Entity
     */
    private UserRaffleOrderEntity convertToEntity(UserRaffleOrder userRaffleOrder) {
        return UserRaffleOrderEntity.builder()
                .id(userRaffleOrder.getId())
                .userId(userRaffleOrder.getUserId())
                .activityId(userRaffleOrder.getActivityId())
                .activityName(userRaffleOrder.getActivityName())
                .strategyId(userRaffleOrder.getStrategyId())
                .orderId(userRaffleOrder.getOrderId())
                .orderTime(userRaffleOrder.getOrderTime())
                .orderStatus(userRaffleOrder.getOrderStatus())
                .createTime(userRaffleOrder.getCreateTime())
                .updateTime(userRaffleOrder.getUpdateTime())
                .build();
    }
}
